package com.siriuscloud.holidayreservation.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siriuscloud.holidayreservation.domain.Appoint;
import com.siriuscloud.holidayreservation.domain.vo.AppointVo;
import com.siriuscloud.holidayreservation.service.AppointService;
import com.siriuscloud.holidayreservation.utils.MyPage;
import com.siriuscloud.holidayreservation.utils.MyPageVo;
import com.siriuscloud.holidayreservation.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
public class AppointController {
    @Resource
    private AppointService appointService;

    @GetMapping("/appoints/all")
    public R list(String applicationNo) {
        List<AppointVo> departments = appointService.getAll(applicationNo);
        return R.ok().data(departments);
    }

    @GetMapping("/appoints/page")
    public R getAllByPage(
            Page<?> page,
            AppointVo appointVo
    ) {
        IPage<AppointVo> allByPage = appointService.getAllByPage(page, appointVo);
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", allByPage.getTotal());
        data.put("rows", allByPage.getRecords());
        return R.ok().data(data);
    }

    @PostMapping("/appoint")
    public R add(@RequestBody AppointVo appointVo) throws Exception {
        Boolean flag = appointService.create(appointVo);
        if (flag) {
            return R.ok().msg("新增成功");
        }
        return R.error().msg("新增失败");
    }

    @PutMapping("/appoint")
    public R update(@RequestBody Appoint appoint) {
        Boolean flag = appointService.update(appoint);
        if (flag) {
            return R.ok().msg("修改成功");
        }
        return R.error().msg("修改失败");
    }

    @DeleteMapping("/appoint/{id}")
    public R delete(@PathVariable String id) {
        Boolean flag = appointService.delete(id);
        if (flag) {
            return R.ok().msg("删除成功");
        }
        return R.error().msg("删除失败");
    }

    @GetMapping("/appoint/{id}")
    public R get(@PathVariable String id) {
        AppointVo appointVo = appointService.getById(id);
        return R.ok().data(appointVo);
    }
}
